<template>
  <section class="component case-single-ratio-adjustment">
    <data-form :model="searchModel" @onSearch="refresh" v-if="!isDetail">
      <template slot="inputs">
        <el-form-item label="账户号" prop="caseNumber">
          <el-input v-model.trim="searchModel.caseNumber"></el-input>
        </el-form-item>
      </template>
    </data-form>
    <el-form :model="model" :rules="rules" label-width="100px" ref="model" v-if="isShow">
      <el-form-item label="账户号" prop="caseNumber" v-if="isDetail">{{
        row.caseNumber
      }}</el-form-item>
      <el-row>
        <el-col :span="8">
          <el-form-item label="客户号">{{ personalId }}</el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="客户姓名">{{ personalName }}</el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="队列名称">{{ queueName }}</el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="经办ID">{{ agentCode }}</el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="调整去向" prop="typeId">
        <el-select clearable placeholder="请选择" v-model="model.typeId" :disabled="isDetail">
          <el-option :key="id" :label="name" :value="id" v-for="{ id, name } in tagList"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="调整理由" prop="note">
        <el-input type="textarea" v-model.trim="model.note" :disabled="isDetail" :maxlength="300" show-word-limit></el-input>
      </el-form-item>
    </el-form>

    <div class="confirm-button">
      <el-button @click="emitClose">{{ isDetail ? "关闭" : "取消" }}</el-button>
      <el-button @click="onClickCommit" :disabled="!isShow" v-if="!isDetail">提交审批</el-button>
    </div>
  </section>
</template>

<script lang="ts">
import { Vue, Component, Emit, Prop, Watch } from "vue-property-decorator";
import { CustomerGroupManageService } from "~/services//domain-service/customer-group-manage.service";
import { Dependencies, Layout } from "~/core/decorator";

@Component({
  components: {}
})
export default class CaseSingleRatioAdjustment extends Vue {
  @Dependencies(CustomerGroupManageService)
  private customerGroupManageService: CustomerGroupManageService;
  @Prop() private isDetail;
  @Prop() private row;
  private model: any = {
    typeId: "",
    note: ""
  };
  private personalId = "";
  private personalName = "";
  private queueName = "";
  private agentCode = "";
  private rules: any = {
    typeId: { required: true, message: "请选择调整去向" },
    note: { required: true, message: "请输入调整理由" }
  };
  private searchModel: any = {
    caseNumber: ""
  };
  private tagList: any = [];
  private isShow: boolean = false;
  @Emit("close") private emitClose () { }
  @Emit("refreshData") private emitRefreshData () { }
  /**
   * 提交
   */
  private onClickCommit () {
    const form: any = this.$refs.model;
    if (!form) return;
    form.validate(valid => {
      if (!valid) return;
      const obj = this.tagList.find(v => v.id === this.model.typeId);
      const params = {
        ...this.model,
        personalId: this.personalId,
        caseNumber: this.searchModel.caseNumber,
        personalName: this.personalName,
        agentCode: this.agentCode,
        queueName: this.queueName
      };
      params.typeName = obj ? obj.name : "";
      this.customerGroupManageService
        .singleAdjustment(params)
        .subscribe(data => {
          this.$message.success("操作成功！");
          this.emitClose();
          this.emitRefreshData();
        });
    });
  }

  private refresh () {
    if (!this.searchModel.caseNumber)
      return this.$message.error("请输入账户号！");
    this.dataHandler(this.searchModel.caseNumber);
  }

  private dataHandler (caseNumber) {
    this.customerGroupManageService
      .getPersonalInfo({ caseNumber })
      .subscribe(data => {
        this.isShow = true;
        this.personalId = data.personalAccount;
        this.personalName = data.personalName;
        this.agentCode = data.agentCode;
        this.queueName = data.queueName;
      });
  }

  private getCaseTagList () {
    this.customerGroupManageService.getEffectTypes().subscribe(data => {
      this.tagList = data;
    });
  }

  private mounted () {
    this.getCaseTagList();
  }
}
</script>
